package com.android2024.no6_music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;

/** 音乐列表界面 **/
public class ListActivity extends AppCompatActivity
        implements View.OnClickListener, AdapterView.OnItemClickListener {
    @Override
    public void onClick(View view) {
        finish();
    }

    //点击listView 中每一项时调用该方法
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //adapterView 适配器的控件列表 view 触发的控件 i 在listview中的位置
        MainActivity.musicData.setChooseMusic(musics.get(i));
        finish();
    }

    //数据适配器
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return musics.size();
        }

        @Override
        public Object getItem(int i) {
            return musics.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null)
                view = View.inflate(ListActivity.this,R.layout.listitem,null);
            musics.get(i).setView(view);
            return view;
        }
    }
    ArrayList<MusicData.Music> musics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        musics = MainActivity.musicData.getMusics();
        ListView listView = findViewById(R.id.listview);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(this);
        findViewById(R.id.list).setOnClickListener(this);
    }
}